﻿using System;
using System.Threading;
using AutoShuntingSchedule.DAL.Models.Sys;
using AutoShuntingSchedule.Utility;
using AutoShuntingSchedule.Utility.Logs;
using AutoShuntingSchedule.Utility.Other;
using SqlSugar;

namespace AutoShuntingSchedule.DAL
{
    /// <summary>
    /// 日志
    /// </summary>
    public class Logger
    {
        private static object _lock = new object();

        private static void Log(string type, string message)
        {
            lock (_lock)
            {
                SimpleClient<SysLog> client = new SimpleClient<SysLog>(BaseLogic.GetInstance());
                SysLog log = new SysLog();
                log.Id = UUID.SnowId;
                log.Type = type;
                log.ThreadId = Thread.CurrentThread.ManagedThreadId;
                log.Message = message;
                log.EnableFlag = "Y";
                log.DeleteFlag = "N";
                log.CreateTime = DateTime.Now;
                log.CreateUserId = "1";
                log.ModifyUserId = "1";
                log.ModifyTime = log.CreateTime;
                client.Insert(log);
            }
        }

        public static void Init(string configContent)
        {
            LogHelper.Init(configContent);
        }

        /// <summary>
        /// 操作日志写入
        /// </summary>
        /// <param name="message"></param>
        public static void OperateInfo(string message)
        {
            Log("Operate", message);
            LogHelper.Operate(message);
        }

        /// <summary>
        /// 运行日志写入
        /// </summary>
        /// <param name="message"></param>
        public static void RunningInfo(string message)
        {
            Log("Running", message);
            LogHelper.Info(message);
        }


        /// <summary>
        /// 错误日志写入
        /// </summary>
        /// <param name="message"></param>
        public static void ErrorInfo(string message)
        {
            Log("Error", message);
            LogHelper.Error(message);
        }
    }
}
